package com.freework.sys.permissions.system.dao;
import java.util.List;

import org.springframework.stereotype.Repository;



import com.freework.Cfg;
import com.freework.freedbm.dao.JdbcForObject;
import com.freework.freedbm.dao.jdbcm.JdbcForDTO;
import com.freework.freedbm.dao.jdbcm.QueryInfo;
import com.freework.freedbm.exception.DAOException;
import com.freework.sys.permissions.system.vo.SysFuncTreeCfg;
import com.freework.sys.permissions.system.vo.SysGroupVO;
import com.freework.sys.permissions.system.vo.SysGroupuserDTO;
import com.freework.sys.permissions.system.vo.UserVO;
import com.freework.sys.permissions.system.vo.SysGroupCfg.field;
import com.freework.tree.TreeDatas;
import com.freework.util.StringUtils;
@Repository  
public class UserDao extends PermissionDAO {



	public void insertGroup(String[] groupid, Integer userid) {
		
		this.deleteUserGroup(userid);
		if(groupid!=null)
		this.insertUserGroup(groupid, userid);
	}
	
	
	public void deleteUser(String[] ids){
		delete(new UserVO(), ids);
		for (int i = 0; i < ids.length; i++) {
			deleteUserGroup(Integer.parseInt(ids[i]));
		}
	}
	
	void deleteUserGroup(Integer userid) {
		String deleteSQL = "delete from sys_groupuser where userid=?";
		super.update(deleteSQL, new Object[] { userid }, Cfg.Integer);
	}
	void insertUserGroup(String[] groupid, Integer userid) {
		SysGroupuserDTO usergroup = new SysGroupuserDTO();
		for (int i = 0; i < groupid.length; i++) {
			usergroup.setUserid(userid);
			usergroup.setGroupid(Integer.parseInt(groupid[i]));
			super.insertByKey(usergroup);
		}

	}
	
	public boolean isUser(Integer userId){
		
		QueryInfo query=new QueryInfo();
		query.setJdbcForDTOs(new JdbcForDTO[] { new JdbcForObject(Cfg.Integer) });
		query.setQuerySelect("select count(*) from sys_user where userid=?");
		query.setParam(Cfg.Integer, userId);
		Integer count=(Integer) getObject(query);
		if(count>0)
			return true;
		else
			return false;
	}
	
	public List<TreeDatas> queryUserFunc(Integer userId) {
		
		String where="  funcid IN(SELECT funcid FROM sys_groupfunc WHERE groupid IN(SELECT groupid FROM sys_groupuser WHERE userid=?))" 
						+" OR " 
						+" funcid IN(SELECT funcid FROM sys_userfunc WHERE userid =?) ";
		QueryInfo qi=QueryInfo.getQueryInfo(SysFuncTreeCfg.cfg, where,userId,userId);
		return query(qi);
		
	}
	public List<SysGroupVO> queryUserGroup(boolean isSelect,Integer userID, Integer cruserID){
		SysGroupVO group=new SysGroupVO();
		String where="groupid "+(isSelect?"IN":" Not In ")+"(SELECT groupid FROM sys_groupuser WHERE userid="+userID+")";
		if(cruserID!=null)
			where+=" and createuser='"+cruserID+"'";
		return	this.query(group,where, field.groupid,field.groupcode,field.groupname);
		
		
	}
	
	public boolean addUser(UserVO userDTO ) throws DAOException{
		userDTO.setUserpassword(StringUtils.password(userDTO.getUserpassword()));
		return this.insertPermission(userDTO, "usercode");
	}
	
	
	
}
